package com.wanli.storemobile.merchant;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.MemberListBean;
import com.wanli.storemobile.event.MemberEvent;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MerchantAddMemberActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IHomePageModel homePageModel;
    private MemberListBean.DataBean.ItemListBean itemListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_add_member);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        itemListBean = (MemberListBean.DataBean.ItemListBean) getIntent().getSerializableExtra("DataBean");
        homePageModel = new HomePageModelImpl();

        if (itemListBean != null) {
            editName.setText(itemListBean.getName());
            editMobile.setText(itemListBean.getMobile());
            titleBar.setTitle("编辑会员信息");
        } else {
            titleBar.setTitle("添加会员信息");
        }
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        String name = editName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showShort("请输入会员姓名");
            return;
        }
        String mobile = editMobile.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showShort("请输入会员手机号");
            return;
        }
        if (itemListBean != null) {
            homePageModel.requestMerchantMemberUpdate(itemListBean.getMember_no(), name, mobile, "", new DataCallBack<BaseResponseBean>() {
                @Override
                public void onSuccessful(BaseResponseBean bean) {
                    ToastUtil.showShort("修改成功");
                    EventBus.getDefault().post(new MemberEvent());
                    finish();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }
    }
}
